package com.atguigu.staservice.service.impl;

import com.atguigu.commonutils.R;
import com.atguigu.staservice.client.UcenterClient;
import com.atguigu.staservice.entity.StatisticsDaily;
import com.atguigu.staservice.mapper.StatisticsDailyMapper;
import com.atguigu.staservice.service.StatisticsDailyService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.RandomUtils;
import org.apache.ibatis.annotations.Case;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 网站统计日数据 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2021-11-25
 */
@Service
public class StatisticsDailyServiceImpl extends ServiceImpl<StatisticsDailyMapper, StatisticsDaily> implements StatisticsDailyService {
    @Autowired
    private UcenterClient ucenterClient;

    @Override
    public void countRegister(String day) {
        QueryWrapper<StatisticsDaily> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("date_calculated", day);
        baseMapper.delete(queryWrapper);

        R countRegister = ucenterClient.countRegister(day);
        Integer count = (Integer) countRegister.getData().get("count");
        Integer loginNum = RandomUtils.nextInt(100, 200);//TODO
        Integer videoViewNum = RandomUtils.nextInt(100, 200);//TODO
        Integer courseNum = RandomUtils.nextInt(100, 200);//TODO

        StatisticsDaily statisticsDaily = new StatisticsDaily();
        statisticsDaily.setRegisterNum(count);
        statisticsDaily.setLoginNum(loginNum);
        statisticsDaily.setVideoViewNum(videoViewNum);
        statisticsDaily.setCourseNum(courseNum);
        statisticsDaily.setDateCalculated(day);
        baseMapper.insert(statisticsDaily);

    }

    @Override
    public Map<String, Object> getStaDaily(String type, String begin, String end) {
        QueryWrapper<StatisticsDaily> queryWrapper = new QueryWrapper<>();
        queryWrapper.between("date_calculated", begin, end);
        queryWrapper.select("date_calculated", type);
        List<StatisticsDaily> DailiesList = baseMapper.selectList(queryWrapper);
        Map<String, Object> getStaDaily = new HashMap<>();
        List<String> dateCalculate = new ArrayList<>();
        List<Integer> typeNum = new ArrayList<>();
        for (int i = 0; i < DailiesList.size(); i++) {
            StatisticsDaily statisticsDaily = DailiesList.get(i);
            dateCalculate.add(statisticsDaily.getDateCalculated());
            switch (type) {
                case "register_num":
                    typeNum.add(statisticsDaily.getRegisterNum());
                case "login_num":
                    typeNum.add(statisticsDaily.getLoginNum());
                default:
                    break;

            }
        }
        getStaDaily.put("dateCalculate", dateCalculate);
        getStaDaily.put("typeNum", typeNum);

        return getStaDaily;
    }
}
